$(function() {
    var curPage = 1;
    var pageSize = 10;
    var consultation = {
        getList: function() {
			$.ajax({
			    url: wsCommon.host + '/api/memberComments/myAsk',
			    dataType: wsCommon.jsonType,
				data: {
                    pageSize: pageSize
                },
			    success: function(res) {
					if(res.code == 1) {
						if(res.result) {
							var pager = res.data.pager;

							consultation.initPaging(pager.pages, pager.total, res);
						} else {
							wsCommon.setCookie('go_back_link', window.location.href);
							window.location.href = './passport-login.html';
						}
					}
			    },
			    error: function() {
			        // console.log('接口返回失败');
			    }
			})
		},
        initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                consultation.randerList(pageData);
                            } else {
                                curPage = page_index + 1;
                                consultation.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                }else {
                    ele.hide();
                    $('..member-comment-list').html('<p style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</p>')
                }
        },
        getPageDates: function() {
            var self = this;

            $.ajax({
                url: wsCommon.host + '/api/memberComments/myAsk',
                dataType: wsCommon.jsonType,
                data: {
					pageNum: curPage,
					pageSize: pageSize
				},
                success: function(res) {

                    // 同步页码
                    var totalPage = res.data.pager.pages;

                    var pageData = res.data.pager.list;
                    var pager = res.data.pager;
                    self.randerList(pageData);
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
        randerList: function(pageData) {
            var tpl = '';

            $.each(pageData, function(index, item) {
                var gaskStr = '';
                if(item.gaskType == 1) {
                    gaskStr = '商品咨询';
                } else if(item.gaskType == 3) {
                    gaskStr = '售后咨询';
                }
                var replyStr = '';
                if(item.replyList == null) {
                    replyStr = '<div class="reply-list">\
                                      <div class="reply-item ">\
                                        <em class="icon lquote">[</em>\
                                        <div class="reply-label"><span class="infos">无</span>\
                                        <em class="icon rquote">\</em>\
                                      </div>\
                                    </div>';
                } else {
                    replyStr += '<div class="reply-list">';
                    $.each(item.replyList, function(indexDir, itemDir) {
                        replyStr += '<div class="reply-item ">\
                                        <em class="icon lquote">[</em>\
                                        <div class="reply-label"><span class="infos"><em>'+itemDir.author+'</em> 说</span><span class="datetime">'+itemDir.commentTime+'</span></div>\
                                        <p class="reply-content">'+itemDir.commentContent+'</p>\
                                        <em class="icon rquote">\</em>\
                                    </div>';
                    })
                    replyStr += '</div>';
                }
                tpl += '<li class="comment-item">\
                          <div class="goods-info">\
                            <div class="goods-pic"><a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'"><img src="'+item.goodsPic+'" alt="'+item.goodsName+'"></a></div>\
                            <a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'"><h3>'+item.goodsName+'</h3></a>\
                            <span class="price">¥'+item.productPrice+'</span>\
                          </div>\
                          <div class="goods-comment">\
                            <div class="comment-infos">\
                              <em>['+gaskStr+']</em>\
                              <div class="datetime">'+item.commentTimeStr+'</div>\
                            </div>\
                            <p class="comments">'+item.commentContent+'</p>\
                            <div class="reply-action">\
                                <span class="inter-handle"><a href="javascript:void(0);" class="action-unfold-reply" rel="21059"><span class="text">回复</span> <q class="icon">6</q></a></span>\
                              </div>'+replyStr+'</div>\
                    </li>';
            })
            $('.member-comment-list ul').html(tpl);
        },
        init: function() {
            this.getList();
        }
    }

    consultation.init();
})
